/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.acme.server;

import java.io.IOException;
import java.util.HashSet;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 *
 * @author iiping
 */
public class ServerAuth implements Filter {

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    boolean isValid = false;
    try{
      String ipAddr = request.getRemoteAddr();
      String authCode = request.getParameter("authenticationCode");
      
      
      if(null != ipAddr && null != authCode ){
        TOServer server = new TOServer(ipAddr, authCode);
        
        isValid = DAOCentral.getInstance().isServerAuthenticated(server);
        
               	

      } 

    }
    catch(Throwable e){
      
    }
    
    if(isValid){
      chain.doFilter(request, response);
    }
    else{
      RequestDispatcher rd = request.getRequestDispatcher("/views/UnAuthResp.jsp");
      rd.forward(request, response);
      
    }

  }

  @Override
  public void destroy() {

  }


}
